package com.bootcamp.javase2.synchro;

import com.sun.corba.se.impl.resolver.FileResolverImpl;

/**
 * @program: day20Sycn
 * @description: use a single thread
 * @author: Keyu Li
 * @create: 2021-09-14 20:49
 **/

public class SingleThreadNotify {
    public static void main(String[] args) {
        PrintNum numRun = new PrintNum();
        new Thread(numRun, "ThreadOdd").start();
        new Thread(numRun, "ThreadEven").start();
    }
}

class PrintNum implements Runnable {
    int index = 1;

    @Override
    public void run() {
        printNum();
    }

    private synchronized void printNum() {
        while (index <= 100) {
            // 唤醒另一个线程，因为synchronized暗含线程锁，所以另一个线程仍然在等待本线程释放监视器对象
            this.notify();
            System.out.println(Thread.currentThread().getName() + "----" + index++);
            try {
                // 本线程释放监视器对象
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}